package com.example.fittrack.service;

import cn.hutool.core.util.IdUtil;
import com.example.fittrack.R.R;
import com.example.fittrack.domain.bo.BasicInfoBo;
import com.example.fittrack.domain.bo.QueryBasicInfoBo;
import com.example.fittrack.domain.vo.BasicInfoVo;
import com.example.fittrack.mapper.BasicInfoMapper;
import com.example.fittrack.utils.DateUtil;
import com.example.fittrack.utils.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class ArchiveService {

    @Autowired
    private BasicInfoMapper basicInfoMapper;

    public R add(BasicInfoBo bo) {

        BasicInfoVo employeeInfoNo = basicInfoMapper.queryByEmployeeNo(bo.getEmployeeNo());
        if (employeeInfoNo != null) {
            return R.FAILED("该档案编号已存在");
        }
        BasicInfoVo employeeNamePhone = basicInfoMapper.queryByEmployee(bo.getEmployeeName(), bo.getPhone());
        if (employeeNamePhone!=null){
            return R.FAILED("该档案姓名手机号已重复");
        }

        BasicInfoBo po = new BasicInfoBo();
        BeanUtils.copyProperties(bo, po);
        po.setId(IdUtil.getSnowflakeNextId());
        po.setCreateTime(DateUtil.currentDate());

        basicInfoMapper.insert(po);

        return R.SUCCESS("操作成功!");
    }

    public R list(QueryBasicInfoBo bo) {
        PageHelper.startPage(bo.getPageNum(), bo.getPageSize());
        List<BasicInfoVo> list = basicInfoMapper.listParam(bo);
        PageInfo<BasicInfoVo> pageInfo = new PageInfo<>(list);
        Page<BasicInfoVo> pageResult = new Page<>(pageInfo.getPageNum(),
                pageInfo.getPageSize(),
                (int) pageInfo.getTotal());
        pageResult.setData(list);
        return R.SUCCESS("操作成功").setData(pageResult);
    }

    public R delete(Long id) {
        basicInfoMapper.deleteArchive(id);
        return R.SUCCESS("操作成功");
    }
}
